<script setup>
import { ref, toRaw, reactive, onActivated } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import Type from '@components/search/type';
import Search from '@components/search/index';
import PageWrapper from '@components/PageWrapper.vue';
import { common, service } from '@utils';
import { Plus, Edit, Delete, View, ArrowDown } from '@element-plus/icons-vue';
import { usePremission } from '@hooks';
import EditCom from './sixthEdit.vue';

const premission = usePremission({
    view: 'sys:role:view',
    add: 'sys:role:add',
    edit: 'sys:role:save',
    delete: 'sys:role:del',
});

const searchs = ref([
    {
        type: Type.SELECT,
        key: 'channel',
        width: 6,
        placeholder: '渠道名称',
        valueKey: 'value',
        data: [],
    },
    {
        width: 6,
        type: Type.SEARCH_BUTTON,
    },
]);

const pageRef = ref();
const tableRef = ref();
const editRef = ref();
let queryForm = {};

const onSearch = param => {
    queryForm = param;
    pageRef.value.refresh();
};
const getStatus = async () => {
    const res = await service.auth.sysdirectListbyCode({ code: 'sms_channel' });
    searchs.value[0].data = res;
};
getStatus();
const getData = async ({ pageNum, pageSize }, cb) => {
    const res = await service.sys.smsConfigList(queryForm);
    cb(res);
    setTimeout(() => {
        tableRef.value.scrollTo(0, 0);
    }, 0);
};
const deleteRow = async row => {
    ElMessageBox.confirm('是否删除此记录', '提示', {
        type: 'warning',
    }).then(async () => {
        const res = await service.sys.smsConfigDelete(row.configId);
        if (res) {
            ElMessage.success('删除成功');
            pageRef.value.refresh(true);
        }
    });
};
const defaultBtn = configId => {
    ElMessageBox.confirm('是否设置此为默认', '提示', {
        type: 'warning',
    }).then(async () => {
        const res = await service.sys.smsConfigDefault(configId);
        if (res) {
            ElMessage.success('设置成功');
            pageRef.value.refresh(true);
        }
    });
};
const width = (function (premission) {
    let width = 24;
    premission.smsEdit && (width += 33);
    premission.smsDelete && (width += 33);
    premission.smsDefault && (width += 64);
    return Math.max(width, 33);
})(premission);
</script>
<template>
    <section>
        <search :items="searchs" @filter="onSearch" />
        <div class="margin-bottom10" v-if="premission.smsAdd">
            <el-button type="primary" :icon="Plus" @click="editRef.open('新增', {})">添加</el-button>
        </div>
        <page-wrapper @get-data="getData" ref="pageRef" :noPage="true">
            <template v-slot:default="props">
                <vxe-table class="mytable-scrollbar" ref="tableRef" :loading="props.loading" :data="props.tableData.records" :max-height="common.tableHeight(true, 2)" highlight-hover-row round stripe border>
                    <!-- <vxe-column type="seq" width="60"></vxe-column> -->
                    <vxe-column field="configId" title="id" width="100" />
                    <vxe-column field="accessKeyId" title="顾客编码" width="180" />
                    <vxe-column field="accessKeySecret" title="校验码" width="180" />
                    <vxe-column field="channelDesc" title="对接渠道" width="180" />
                    <vxe-column field="endpoint" title="访问域名" width="180" />
                    <vxe-column field="region" title="城市名" width="180" />
                    <vxe-column field="signName" title="签名" width="180" />

                    <vxe-column title="操作" fixed="right" :min-width="width" align="center">
                        <template #default="{ row }">
                            <section class="optBtn">
                                <span @click="editRef.open('编辑', row)" v-if="premission.smsEdit">编辑</span>
                                <span @click="deleteRow(row)" v-if="premission.smsDelete">删除</span>
                                <span @click="defaultBtn(row.configId)" v-if="!row.defaultConfig && premission.smsDefault">设为默认</span>
                            </section>
                        </template>
                    </vxe-column>
                </vxe-table>
            </template>
        </page-wrapper>
        <edit-com ref="editRef" @refresh="flag => pageRef.refresh(flag)" :status="searchs[0].data" />
    </section>
</template>

<style lang="scss">
.tree-border {
    margin-top: 5px;
    border: 1px solid #e5e6e7;
    background: #ffffff none;
    border-radius: 4px;
    width: 100%;
}
.optBtn {
    display: flex;
    justify-content: space-evenly;
    span {
        cursor: pointer;
        color: orangered;
    }
    color: #ccc;
}
:deep(.el-dropdown) {
    margin-top: 0;
}
.el-dropdown-link {
    cursor: pointer;
    //   color: var(--el-color-primary);
    display: flex;
    align-items: center;
    font-size: 13px;
}
</style>
